package com.ruoyi.ai.factory;

import com.ruoyi.ai.domain.vo.WxModelConfigVo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ModelChatStrategyFactory {
    //本地缓存配置
    private static final Map<String, WxModelConfigVo> MODEL_CACHE = new ConcurrentHashMap<>();

    private static String ENABLE_MODEL_MFG;

    private static Map<String, ModelChatService> services = new ConcurrentHashMap<>();

    public static ModelChatService getByMfg(String mfg) {
        return services.get(mfg);
    }

    public static ModelChatService getEnableMfg() {
        if (StringUtils.isEmpty(ENABLE_MODEL_MFG)) {
            throw new ServiceException("未选择模型");
        }
        return services.get(ENABLE_MODEL_MFG);
    }

    public static void updateModelCache(List<WxModelConfigVo> wxModelConfigVos) {
        for (WxModelConfigVo wxModelConfigVo : wxModelConfigVos) {
            MODEL_CACHE.put(wxModelConfigVo.getMfg(), wxModelConfigVo);
            if (wxModelConfigVo.getEnable() == 1) {
                ENABLE_MODEL_MFG = wxModelConfigVo.getMfg();
            }
        }
    }

    public static void register(String type, ModelChatService modelChatService) {
        services.put(type, modelChatService);
    }

}
